package com.rogo.log
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import spark.formatters.DateTimeFormatter;
	
	/**
	 * 程序日志文件
	 * 
	 * @author Rogo
	 */	
	public class AppLogFile
	{
		public function AppLogFile()
		{
			dateFormatter = new DateTimeFormatter();
			dateFormatter.dateTimePattern = 'yyyy-MM-dd';
		}
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var dateFormatter:DateTimeFormatter;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 保存日志信息
		 * 
		 * @param msg
		 */		
		public function saveLog(msg:String):void
		{
			var logFile:File = getLogFile();
			var fs:FileStream = new FileStream();
			fs.open(logFile, FileMode.APPEND);
			fs.writeUTFBytes(msg + '\n');
			fs.close();
		}
		
		/**
		 * 获取日志文件
		 * 
		 * @return 
		 */		
		private function getLogFile():File
		{
			var logDir:File = File.applicationStorageDirectory.resolvePath('Logs');
			logDir.createDirectory();
			
			var fileName:String = dateFormatter.format(new Date()) + '.log';
			var logFile:File = logDir.resolvePath(fileName);
			return logFile;
		}
	}
}